package component.untils
{
	import common.core.*;
	
	import component.show.containers.Image;
	import component.show.containers.SimpleBtn;
	import component.show.containers.ToggleButton;
	import component.show.containers.UIFrame;
	import component.show.containers.tree.Tree;
	
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.DataGrid;
	import fl.controls.Label;
	import fl.controls.List;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.controls.dataGridClasses.DataGridColumn;
	
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public class ComponentXMLAdapter
	{
		private var m_targetComItem : ComponentItem;
		
		public function ComponentXMLAdapter()
		{
		
		}
		
		public function destory() : void
		{
			m_targetComItem = null;
		}
		
		public function set target(value : ComponentItem) : void
		{
			m_targetComItem = value;
		}
		
		public function get target() : ComponentItem
		{
			return m_targetComItem;
		}
		
		public function getXML() : XML
		{
			var itemXML : XML;
			
			var m_target : * = m_targetComItem.item;
			
			switch(true)
			{
				case m_target is UIFrame:
					itemXML = <Frame name='' x='' y='' width='' height='' scale9Grid='' skin='' filterName=''></Frame>;
					
					var scale9GridStr : String = "";
					var rec : Rectangle = (m_target as UIFrame).scale9Grid;
					
					if(rec)
					{
						scale9GridStr += rec.top;
						scale9GridStr += ",";
						scale9GridStr += rec.left;
						scale9GridStr += ",";
						scale9GridStr += rec.width;
						scale9GridStr += ",";
						scale9GridStr += rec.height;
					}
					
					itemXML.@scale9Grid = scale9GridStr;
					itemXML.@skin = (m_target as UIFrame).skinName;
					
					break;
				case m_target is Button:
					itemXML = <Button name='' x='' y='' width='' height='' htmlText='' filterName=''></Button>;
					itemXML.@htmlText = (m_target as Button).label;
					break;
				case m_target is CheckBox:
					itemXML = <CheckBox name='' x='' y='' width='' height='' htmlText='' filterName=''></CheckBox>;
					itemXML.@htmlText = (m_target as CheckBox).label;
					break;
				case m_target is List:
					itemXML = <List name='' x='' y='' width='' height=''></List>;
					break;
				case m_target is Label:
					itemXML = <TextField name='' x='' y='' width='' height='' htmlText='' TextFormat='' filterName=''></TextField>;

					var txt : String = (m_target as Label).text;
				
					if(m_targetComItem.txtId)
					{
						itemXML.@htmlText =m_targetComItem.txtId;
					}
					else
					{
						itemXML.@htmlText =txt;
					}

					break;
				case m_target is TextInput:
					itemXML = <TextInput name='' x='' y='' width='' height='' htmlText='' filterName=''></TextInput>;
					itemXML.@htmlText = (m_target as TextInput).text;
					break;
				case m_target is TextArea:
					itemXML = <TextArea name='' x='' y='' width='' height='' htmlText='' filterName=''></TextArea>;
					itemXML.@htmlText = (m_target as TextArea).text;
					break;
				case m_target is Image:
					itemXML = <Image name='' x='' y='' width='' height='' skin='' filterName=''></Image>;
					itemXML.@skin = (m_target as Image).skinName;
					break;
				case m_target is DataGrid:
					itemXML = <DataGrid name='' x='' y='' width='' height='' columns='' filterName=''></DataGrid>;
					
					var colStr : String = "";
					var dg : DataGrid = m_target as DataGrid;
					
					var dgColLst : Array = dg.columns;
					var length : int = dgColLst.length;
					
					for(var i : int=0 ;i< length ; i++)
					{
						var colItem : DataGridColumn = dgColLst[i];
						
						var colItemStr : String = "[";
						
						colItemStr += colItem.headerText;
						colItemStr +=",";
						colItem.resizable == true ? colItemStr+="1" : colItemStr+="0";
						colItemStr +=",";
						colItem.sortable == true ? colItemStr+="1" : colItemStr+="0";
						
						colItemStr += "]";
						
						colStr += colItemStr;
					}
					
					itemXML.@colums = colStr;
					break;
				case m_target is SimpleBtn:
					itemXML = <SimpleBtn name='' x='' y='' width='' height='' skin='' filterName=''></SimpleBtn>;
					itemXML.@skin = (m_target as SimpleBtn).skinName;
					break;
				case m_target is ToggleButton:
					itemXML = <ToggleButton name='' x='' y='' width='' height='' skin='' filterName=''></ToggleButton>;
					itemXML.@skin = (m_target as ToggleButton).skinName;
					break;
				case m_target is Tree:
					itemXML = <Tree name='' x='' y='' width='' height=''></Tree>;
					break;
				case m_target is ScrollPane:
					itemXML = <ScrollPane name='' x='' y='' width='' height='' filterName=''></ScrollPane>;
					break;
				default : 
					itemXML = <Item name='' x='' y='' width='' height='' filterName=''></Item>;
					break;
			}
			
			return itemXML;
		}
	}
}